package com.web.model;

import com.alibaba.fastjson.JSONArray;
import com.jfinal.plugin.activerecord.Model;
import org.apache.log4j.Logger;

/**
 * Created by reeveslee on 16/7/3.
 */
public class UserModel extends Model<UserModel> {
    private static final Logger LOGGER = Logger.getLogger(UserModel.class);
    public static UserModel userDao = new UserModel();

    public UserModel getUserInfo(String openId) {
        String sql = "select id,token,photo,name,college,sex,wechatAccount,telnumber,province,city,slogan from v1_grass_user where openId=?";
        return userDao.findFirst(sql, openId);
    }

    /**
     * 添加用户
     */
    public long addUser(String openId, String access_token, String refresh_token, String name, String sex, String province, String city, String photo) {
        try {
            UserModel newUser = new UserModel();
            newUser.set("openId", openId).set("token", access_token).set("name", name).set("sex", sex).set("province", province).set("city", city).set("photo", photo).set("refreshToken", refresh_token).save();
            return newUser.getLong("id");
        } catch (Exception e) {
            LOGGER.error("新建用户错误: " + e);
            return -1;
        }
    }

    public boolean changeUserInfo(int userId, String college, String wechatAccount, String telnumber, String slogan, String invite, String runapp) {
        userDao.findById(userId).set("college", college).set("wechatAccount", wechatAccount).set("telnumber", telnumber).set("slogan", slogan).set("invite", invite).set("runapp", runapp).update();
        return true;
//        try {
//
//            return true;
//        } catch (Exception e) {
//            return false;
//        }
    }
}
